<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="设备sn" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入设备sn"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备mac" prop="sn">
        <el-input
          v-model="queryParams.mac"
          placeholder="请输入设备mac"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入账代理商名称" prop="sn">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入入账代理商名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收益时间" prop="timeId">
        <el-input
          v-model="queryParams.timeId"
          placeholder="请输入收益时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算运营商" prop="settleLsp">
        <el-input
          v-model="queryParams.settleLsp"
          placeholder="请输入结算运营商"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button icon="Download" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['producer:earnings:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['producer:earnings:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['producer:earnings:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['producer:earnings:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row> -->
    <div style="padding: 10px;">总收益：{{ nums }}</div> 
    <el-table v-loading="loading" :data="earningsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="设备sn"  width="200" align="center" prop="sn" v-if="columns[0].visible" />
        <el-table-column label="设备mac"  width="150" align="center" prop="mac" v-if="columns[0].visible" />
        <el-table-column label="收益时间" width="150" align="center" prop="timeId" v-if="columns[1].visible"/>
        <el-table-column label="实际到账收益" width="150" align="center" prop="realIncome" v-if="columns[2].visible"/>

        <el-table-column label="当日在线点数" width="150" align="center" prop="onlineCount" v-if="columns[2].visible"/>
        <el-table-column label="95计费带宽(单位:Mbps)" width="200" align="center" prop="settleBw" v-if="columns[2].visible"/>
        <el-table-column label="入账代理商" width="200" align="center" prop="agentName" v-if="columns[2].visible"/>
        

        <el-table-column label="收益业务方" width="150" align="center" prop="deviceCloud" v-if="columns[2].visible"/>
        <el-table-column label="贡献收益" width="150" align="center" prop="contributeIncome" v-if="columns[3].visible"/>
        <el-table-column label="sla扣减金额" width="150" align="center" prop="deductCash" v-if="columns[4].visible"/>
        <el-table-column label="其他收益" width="150" align="center" prop="otherIncome" v-if="columns[5].visible"/>
        <el-table-column label="结算带宽" width="150" align="center" prop="settleBw" v-if="columns[6].visible"/>
        <el-table-column label="结算运营商" width="150" align="center" prop="settleLsp" v-if="columns[7].visible"/>
        <el-table-column label="备注" width="150" align="center" prop="remark" v-if="columns[8].visible"/>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['producer:earnings:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['producer:earnings:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备收益详情对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="earningsRef" :model="form" :rules="rules">
        <el-form-item label="设备sn" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入设备sn" />
        </el-form-item>
        <el-form-item label="时间" prop="timeId">
          <el-input v-model="form.timeId" placeholder="请输入时间" />
        </el-form-item>
        <el-form-item label="实际到账收益" prop="realIncome">
          <el-input v-model="form.realIncome" placeholder="请输入实际到账收益" />
        </el-form-item>
        <el-form-item label="贡献收益" prop="contributeIncome">
          <el-input v-model="form.contributeIncome" placeholder="请输入贡献收益" />
        </el-form-item>
        <el-form-item label="sla扣减金额" prop="deductCash">
          <el-input v-model="form.deductCash" placeholder="请输入sla扣减金额" />
        </el-form-item>
        <el-form-item label="其他收益" prop="otherIncome">
          <el-input v-model="form.otherIncome" placeholder="请输入其他收益" />
        </el-form-item>
        <el-form-item label="结算带宽" prop="settleBw">
          <el-input v-model="form.settleBw" placeholder="请输入结算带宽" />
        </el-form-item>
        <el-form-item label="结算运营商" prop="settleLsp">
          <el-input v-model="form.settleLsp" placeholder="请输入结算运营商" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Earnings">
import { listEarnings,totalIncome, getEarnings, delEarnings, addEarnings, updateEarnings } from "@/api/producer/earnings"

const { proxy } = getCurrentInstance()
const nums = ref(0)
const earningsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

// 列显隐信息
const columns = ref([
{ key: 0, label: `设备sn`, visible: true },
{ key: 1, label: `时间`, visible: true },
{ key: 2, label: `实际到账收益（元）`, visible: true },
{ key: 3, label: `贡献收益（元）`, visible: true },
{ key: 4, label: `sla扣减金额（元）`, visible: true },
{ key: 5, label: `其他收益（元）`, visible: true },
{ key: 6, label: `结算带宽`, visible: true },
{ key: 7, label: `结算运营商`, visible: true },
{ key: 8, label: `备注`, visible: true },
])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sn: null,
    mac: null,
    timeId: null,
    agentName:null,
    realIncome: null,
    contributeIncome: null,
    deductCash: null,
    otherIncome: null,
    settleBw: null,
    settleLsp: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询设备收益详情列表 */
function getList() {
  loading.value = true
  listEarnings(queryParams.value).then(response => {
    earningsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
 
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    sn: null,
    mac: null,
    timeId: null,
    realIncome: null,
    contributeIncome: null,
    deductCash: null,
    otherIncome: null,
    settleBw: null,
    settleLsp: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("earningsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
  console.log(queryParams.value)
  totalIncome(queryParams.value).then(response => {
    nums.value = response.data
  })
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    sn: null,
    mac: null,
    timeId: null,
    agentName: null,
    realIncome: null,
    contributeIncome: null,
    deductCash: null,
    otherIncome: null,
    settleBw: null,
    settleLsp: null,
  };
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加设备收益详情"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getEarnings(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改设备收益详情"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["earningsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateEarnings(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addEarnings(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除设备收益详情编号为"' + _ids + '"的数据项？').then(function() {
    return delEarnings(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('producer/earnings/export', {
    ...queryParams.value
  }, `earnings_${new Date().getTime()}.xlsx`)
}

getList()
</script>
